import 'package:flutter/material.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:get/get.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/controller/user.dart';
import 'package:ichinesereader/models/common/pageLabelIcon.dart';
import 'package:ichinesereader/pages/student/studentHomeHiLevel.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:ichinesereader/widgets/dialogs/showStudentNewAssessment.dart';
import 'package:ichinesereader/widgets/layouts/appdrawerStudent.dart';
import 'package:ichinesereader/widgets/layouts/customAppBar.dart';
import 'package:intl/intl.dart';
import '../../widgets/buttons/floatingActionSideMenu.dart';

class StudentHome extends StatefulWidget {
  static const String routeName = '/student/home';

  @override
  _StudentHomeState createState() => _StudentHomeState();
}

class _StudentHomeState extends State<StudentHome> {
  final List<PageLabelIcon> pages = [
    PageLabelIcon(
        route: '/student/leveledreadings',
        icon: Icons.trending_up,
        label: 'Leveled Readings'.tr),
    PageLabelIcon(
        route: '/student/myfavorites',
        icon: Icons.favorite_border_rounded,
        label: 'Favorites'.tr),
    PageLabelIcon(
        route: '/student/homework',
        icon: Icons.task_rounded,
        label: 'Homework'.tr),
    PageLabelIcon(
        route: '/student/assessments',
        icon: Icons.assessment_rounded,
        label: 'Assessments'.tr),
  ];

  void onTap(int index) {
    Get.offAndToNamed(pages[index].route.toString());
  }

  final UserController _userController = Get.find();
  var box = Hive.box('appconfig');
  bool isLowLevel = false;

  @override
  void initState() {
    bool isDark = box.get('theme', defaultValue: 0) == 1;
    // the following code cause home build two times
    _userController.getProfile().then((value) {
      // if teacher has set interfaceChinese is true, only show chinese
      Future.delayed(Duration(milliseconds: 500), () {
        bool interfaceChinese = box.get('interfaceChinese') ?? true;
        int defaultLocal = 0;
        // String _userRole = box.get(CacheManagerKey['nanhairole'].toString());
        if (interfaceChinese == true) {
          defaultLocal = 1;
        }
        int locale = box.get('locale', defaultValue: defaultLocal);
        if (locale == 0) {
          Get.updateLocale(Locale('en', 'US'));
        }
        if (locale == 1) {
          Get.updateLocale(Locale('zh', 'CN'));
        }
        if (locale == 2) {
          Get.updateLocale(Locale('zhHant', 'TW'));
        }
      });
    });
    Future.delayed(Duration(milliseconds: 300), () {
      if (isDark) {
        Get.changeThemeMode(ThemeMode.dark);
      } else {
        Get.changeThemeMode(ThemeMode.light);
      }
    });

    Future.delayed(Duration(milliseconds: 200), () {
      String notshowtoday =
          box.get('notshowtoday', defaultValue: '').toString();
      bool notshow =
          notshowtoday == DateFormat('yyyyMMdd').format(DateTime.now());
      _userController.getNewAssessmentAccount().then((assessmentNum) {
        if (assessmentNum > 0 && notshow == false) {
          showGeneralDialog(
              context: context,
              transitionDuration: Duration(milliseconds: 400),
              pageBuilder: (bc, ania, anis) {
                return ShowStudentNewAssessmentDialog();
              });
        }
      });
    });

    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    Log.debug('start building student home page');

    final isSmallScreen =
        MediaQuery.of(context).size.width < Constants.DefinedScreenSmallSize;

    return Scaffold(
      backgroundColor: Get.isDarkMode ? Color(0xff212121) : Colors.transparent,
      appBar: isSmallScreen ? CustomAppBar(title: 'Home'.tr) : null,
      drawer: isSmallScreen ? StudentAppDrawer() : null,
      floatingActionButton: FloatingActionButtonSideMenu(),
      body: StudentHomeHiLevel(),
      // Obx(() {
      //   if (_userController.profile.value.userName != null) {
      //     var level = int.tryParse(_userController.profile.value.level
      //             .toString()
      //             .replaceAll('level', '')) ??
      //         1;
      //     if (level > 0 && level < 6) isLowLevel = true;

      //     box.put('UserLevel', level.toString());

      //     return StudentHomeHiLevel();
      //   } else {
      //     return Text('');
      //   }
      // }),
      // // bottomNavigationBar: GetPlatform.isAndroid || GetPlatform.isIOS
      // //     ? BottomNavigationBar(
      // //         type: BottomNavigationBarType.fixed,
      // //         onTap: onTap,
      // //         selectedItemColor: Colors.pinkAccent,
      // //         unselectedItemColor: Colors.grey.withOpacity(0.5),
      // //         showUnselectedLabels: false,
      // //         showSelectedLabels: false,
      // //         items: pages
      // //             .map((page) => BottomNavigationBarItem(
      // //                   label: page.label,
      // //                   icon: Icon(page.icon),
      // //                 ))
      // //             .toList(),
      // //       )
      // //     : null,
    );
  }
}
